package com.frank.subject.application.controller;

import com.frank.subject.common.entity.Result;
import com.frank.subject.application.convert.SubjectInfoDtoConverter;
import com.frank.subject.application.dto.SubjectInfoDto;
import com.frank.subject.common.entity.PageResult;
import com.frank.subject.domain.entity.SubjectInfoBo;
import com.frank.subject.domain.service.SubjectDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author Climb Zou
 * @version v0.0.1
 * @date 2024/1/19 15:56
 * @description 刷题控制器
 */
@Slf4j
@RestController
@RequestMapping("/subject")
public class SubjectController {

    private final SubjectDomainService subjectDomainService;

    public SubjectController(SubjectDomainService subjectDomainService) {
        this.subjectDomainService = subjectDomainService;
    }


    @PostMapping(value = "/add")
    public Result<Boolean> addSubject(@RequestBody SubjectInfoDto subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Add subject, subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(), "Subject name is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "Subject difficult is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "Subject type is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "Subject score is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectParse(), "Subject parse is be empty!");
            Preconditions.checkArgument(!subjectInfoDTO.getLabelIdList().isEmpty(), "Subject label is be empty!");
            Preconditions.checkArgument(!subjectInfoDTO.getCatetoryIdList().isEmpty(), "Subject category is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("Add subject error, Error: {}", e.getMessage(), e);
            return Result.fail();
        }
    }

    @GetMapping("/getSubjectPage")
    public Result<PageResult<SubjectInfoDto>> getSubjectPage(SubjectInfoDto subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Get subject page, subjectInfoDTO: {}", subjectInfoDTO);
            }
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "Label id is be empty!");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "Category id is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            PageResult<SubjectInfoBo> subjectInfoBoPageResult = subjectDomainService.getSubjectPage(subjectInfoBO);
            PageResult<SubjectInfoDto> subjectInfoDtoPageResult = SubjectInfoDtoConverter.INSTANCE.convertBoPageToDtoPageResult(subjectInfoBoPageResult);
            return Result.ok(subjectInfoDtoPageResult);
        } catch (Exception exception) {
            log.error("Get subject page error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }

    @GetMapping("/querySubjectInfo")
    public Result<SubjectInfoDto> querySubjectInfo(SubjectInfoDto subjectInfoDto) {
        try {
            if (log.isInfoEnabled()) {
                log.info("Get subject info, subjectInfoDTO: {}", subjectInfoDto);
            }
            Preconditions.checkNotNull(subjectInfoDto.getId(), "Subject id is be empty!");
            SubjectInfoBo subjectInfoBO = SubjectInfoDtoConverter.INSTANCE.convertDtoToBo(subjectInfoDto);
            SubjectInfoBo resultBo = subjectDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDto result = SubjectInfoDtoConverter.INSTANCE.convertBoToDto(resultBo);
            return Result.ok(result);
        } catch (Exception exception) {
            log.error("Get subject info error, Error: {}", exception.getMessage(), exception);
            return Result.fail();
        }
    }


}
